﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using Microsoft.VisualStudio.Shell;
using UCL.Powersource;

namespace PowersourceUIToolkit
{


    public class UIManager : UIManagerBase
    {
        public UIManager()
        {
            Initialize();
        }
        
        internal IViewConductor GetToolWindowPaneConductor(ToolWindowPane twp)
        {
            var twpc = ViewConductors.FirstOrDefault(a => { var b = a as ToolWindowPaneConductor; return (b != null ? (b.ToolWindowPane == twp) : false); });
            if (twpc == null)
            {
                twpc = new ToolWindowPaneConductor(twp);
                ViewConductors.Add(twpc);
            }
            return twpc;
        }

        protected override bool OnInitializing()
        {
            return true;
        }

        public override IViewConductor GetViewConductor<T>(T rootControl) 
        {
            if (rootControl is ToolWindowPane) return GetToolWindowPaneConductor((ToolWindowPane)(object)rootControl);
            if (rootControl is ContentControl) return GetContentControlConductor((ContentControl)(object)rootControl);
            return null;
        }

        private IViewConductor GetContentControlConductor(ContentControl rootControl)
        {
            var ccc = ViewConductors.FirstOrDefault(a => { var b = a as ContentControlConductor; return (b != null ? (b.RootControl == rootControl) : false); });
            if (ccc == null)
            {
                ccc = new ContentControlConductor(rootControl);
                ViewConductors.Add(ccc);
            }
            return ccc;
        }



        public override Type GetViewType<T>(T rootModel) 
        {
            return ConventionManager.GetViewType(rootModel.GetType());
        }


        public override void Bind(IViewModel vm, System.Windows.FrameworkElement iview, object context)
        {
            ViewModelBinder.Bind(vm, iview, null);
        }
    }
}
